package com.licw.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.licw.constant.MessageConstant;
import com.licw.constant.StatusConstant;
import com.licw.dto.SetmealDTO;
import com.licw.dto.SetmealPageQueryDTO;
import com.licw.entity.Dish;
import com.licw.entity.Setmeal;
import com.licw.entity.SetmealDish;
import com.licw.exception.DeletionNotAllowedException;
import com.licw.exception.SetmealEnableFailedException;
import com.licw.mapper.DishMapper;
import com.licw.mapper.SetmealDishMapper;
import com.licw.mapper.SetmealMapper;
import com.licw.result.PageResult;
import com.licw.service.SetmealService;
import com.licw.vo.DishItemVO;
import com.licw.vo.SetmealVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Service
public class SetmealServiceImpl implements SetmealService {


    @Resource
    private DishMapper dishMapper;
    @Resource
    private SetmealMapper setmealMapper;

    @Resource
    private SetmealDishMapper setmealDishMapper;


    @Override
    @Transactional
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public void save(SetmealDTO setmealDTO) {
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insert(setmeal);

        if(CollectionUtils.isEmpty(setmealDishes)) return;
        Long setmealId = setmeal.getId();
        for(SetmealDish setmealDish : setmealDishes){
            setmealDish.setSetmealId(setmealId);
        }
        setmealDishMapper.insertBatch(setmealDishes);
    }

    @Override
    public SetmealVO getVOById(Long id) {
        // 查询基础套餐信息
        SetmealVO setmealVO = setmealMapper.selectById(id);

        // 查询套餐对应的菜品
        List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    public PageResult<SetmealVO> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> setmealPage = setmealMapper.pageSelect(setmealPageQueryDTO);
        long total = setmealPage.getTotal();
        List<SetmealVO> result = setmealPage.getResult();
        return new PageResult<>(total, result);
    }

    @Override
    @Transactional
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public void deleteByIds(List<Long> ids) {
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.getById(id);
            if(StatusConstant.ENABLE.equals(setmeal.getStatus())){
                //起售中的套餐不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });

        // 删除套餐信息
        setmealMapper.deleteBatchByIds(ids);
        // 删除套餐和菜品关联
        setmealDishMapper.deleteBatchBySetmealIds(ids);
    }

    @Override
    @Transactional
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public void update(SetmealDTO setmealDTO) {
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        // 修改基本套餐信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);
        // 删除原本套餐——菜品关联
        Long setmealId = setmealDTO.getId();
        setmealDishMapper.deleteBatchBySetmealIds(Collections.singletonList(setmealId));
        // 新增现在套餐——菜品关联
        if(CollectionUtils.isEmpty(setmealDishes)) return;
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }
        setmealDishMapper.insertBatch(setmealDishes);
    }

    @Override
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public void setStatus(Integer status, Long id) {
        //起售套餐时，判断套餐内是否有停售菜品，有停售菜品提示"套餐内包含未启售菜品，无法启售"
        if(Objects.equals(status, StatusConstant.ENABLE)){
            List<Dish> dishList = dishMapper.getBySetmealId(id);
            if(dishList != null && dishList.size() > 0){
                dishList.forEach(dish -> {
                    if(StatusConstant.DISABLE.equals(dish.getStatus())){
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }

        Setmeal setmeal = Setmeal.builder().id(id).status(status).build();
        setmealMapper.update(setmeal);
    }

    @Override
    @Cacheable(cacheNames = "setmealCache",key = "#setmeal.categoryId")
    public List<Setmeal> list(Setmeal setmeal) {
        return setmealMapper.list(setmeal);
    }
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
